<?php

namespace App\Http\Controllers\Admin;

use App\Models\Admin\Topic;
use App\Models\RTresult;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;


class TopicController extends Controller
{
    //将数据库的话题表的数据遍历到话题页面
    public function index()
    {
        //分页，基于bootstrap
        $topics = Topic::paginate(3);
//        $topics = Topic::all();
        return view('admin.topic', ['topics' => $topics]);
    }
    //话题的搜索分页
    public function page(Request $request){
        //默认的搜索框为post传值
        if ($request->isMethod('post')) {
            //获取填写的内容
            $contents = $request->contents;
            //得到数据库中的数据
            $db = Topic::all();
            //定义一个数组，用来保存拼接的语句
            $arr = array();
            //拼接原生的sql语句，1为true`
            $where = 1;
            //判断输入的内容是否存在，用来搜索
            if (!empty($contents)) {
                //拼接原生的sql查询条件  使用模糊查询
                $where .= " and topic like \"%" . $contents . "%\"";
                //拼接多条件查询
                $arr ['topic'] = "topic like \"%" . $contents . "%\"";
            }
            //是否可见的请求值
            $is_gai = $request->is_gai;
//            dd($is_gai);
            //菜单的值不为空
            if ($is_gai == 1 || $is_gai == 0) {

                // +   拼接  菜单 查询语句
                $where .= " and is_show =" . $is_gai;
                //拼接  菜单  查询条件
                $arr ['is_show'] = $is_gai;

            }
            //是否热门的请求值
            $is_hot = $request->is_hot;
//            dd($is_gai);
            //菜单的值不为空
            if ($is_hot == 1 || $is_hot == 0) {

                // +   拼接  菜单 查询语句
                $where .= " and is_hot =" . $is_hot;
                //拼接  菜单  查询条件
                $arr ['is_hot'] = $is_hot;

            }
            $db = Topic::whereRaw($where);

            $topics = $db->paginate(3);
            //appends把条件追加到URL里面
            $topics = $topics->appends($arr);
            return view('admin/topic', ['topics' => $topics]);
        }
        /***get的传递方式用来分页查询点击下一页***/
        $arr = array();
        $where = 1;
        //get的接收方式
        if (!empty($_GET['topic'])) {

            $where .= " and " . $_GET['topic'];
            $arr ['topic'] = $_GET['topic'];
        }
        $is_gai = $request->is_gai;
        $show = $_GET['is_show'];
        if ($show == 1 || $show == 0) {

            $where .= " and is_show =" . $show;
            $arr ['is_show'] = $show;

        }
        $is_hot = $request->is_hot;
        $hot = $_GET['is_hot'];
        if ($hot == 1 || $hot == 0) {

            $where .= " and is_hot =" . $hot;
            $arr ['is_hot'] = $hot;

        }
        //原生sql拼接语句
        $db = Topic::whereRaw($where);
        //分页
        $topics = $db->paginate(3);
        //appends把条件追加到URL里面
        $topics = $topics->appends($arr);

        return view('admin/topic', ['topics' => $topics]);

    }

    /*********************************************************************************/
    //将话题的{开启禁用}写出来
    public function status_topic(Request $request)
    {
        //调用状态值
        $rt_result = new RTresult();
        //判断是否  显示 或者 禁用
        $is_show = $request->is_show;
        //获取  话题表 的ID
        $topic_id = $request->topic_id;
        if ($is_show == 1) {
            $is_show = 0;
        } else {
            $is_show = 1;
        }
        //根据  点击所在的ID  改变是否显示或者禁用
        $res = Topic::find($topic_id)->update(['is_show' => $is_show]);

        if ($res) {
            $rt_result->status = true;
            $rt_result->message = $is_show;
            return $rt_result->toJson();
        }
    }
    //将话题的{开启禁用}写出来
    public function hot_topic(Request $request)
    {
        //调用状态值
        $rt_result = new RTresult();
        //判断是否  显示 或者 禁用
        $is_hot = $request->is_hot;
        //获取  话题表 的ID
        $topic_id = $request->topic_id;
        if ($is_hot == 1) {
            $is_hot = 0;
        } else {
            $is_hot = 1;
        }
        //根据  点击所在的ID  改变是否显示或者禁用
        $res = Topic::find($topic_id)->update(['is_hot' => $is_hot]);

        if ($res) {
            $rt_result->status = true;
            $rt_result->message = $is_hot;
            return $rt_result->toJson();
        }
    }
    /*******************************************************************************/
    //话题 的 详情表
    public function show($id)
    {
        //取出详情对应的数据，两表联查
        $topics = Topic::join('weibos', 'topics.id', '=', 'weibos.topic_id')
            ->select('weibos.*', 'topics.topic')
            //取出id指定的一条或者几条数据
            ->where('weibos.topic_id', '=', $id)
            ->get();
//        dd($topics);
        return view('admin.topic_show')->with('topics', $topics);
    }
    //添加话题的视图
     public function create(){

          return view('admin.topic_create');
      }
      //添加话题到数据库
      public function store(Request $request)
      {
          $topic = new Topic();
          $topic->topic = $request->topic;
          if ($topic->save()) {
              return redirect('admin/topic');
          } else {
              return redirect()->back();

          }
      }

}
